<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="绑定用户的主键ID" prop="servedUserid">
        <el-input
          v-model="queryParams.servedUserid"
          placeholder="请输入绑定用户的主键ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="servedName">
        <el-input
          v-model="queryParams.servedName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="servedIdnumber">
        <el-input
          v-model="queryParams.servedIdnumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="servedTelephonenumber">
        <el-input
          v-model="queryParams.servedTelephonenumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就诊卡号" prop="servedVisitid">
        <el-input
          v-model="queryParams.servedVisitid"
          placeholder="请输入就诊卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户性别" prop="servedSex">
        <el-select v-model="queryParams.servedSex" placeholder="请选择用户性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="与被服务人关系" prop="servedConnect">
        <el-input
          v-model="queryParams.servedConnect"
          placeholder="请输入与被服务人关系"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="要求服务日期">
        <el-date-picker
          v-model="daterangeServedDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="要求服务时间">
        <el-date-picker
          v-model="daterangeServedTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="创建者" prop="servedCreateBy">
        <el-input
          v-model="queryParams.servedCreateBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeServedCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="更新者" prop="servedUpdateBy">
        <el-input
          v-model="queryParams.servedUpdateBy"
          placeholder="请输入更新者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间">
        <el-date-picker
          v-model="daterangeServedUpdateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="启用标志" prop="servedUseFlag">
        <el-select v-model="queryParams.servedUseFlag" placeholder="请选择启用标志" clearable>
          <el-option
            v-for="dict in dict.type.service_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="删除标志" prop="servedDelFlag">
        <el-select v-model="queryParams.servedDelFlag" placeholder="请选择删除标志" clearable>
          <el-option
            v-for="dict in dict.type.service_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="默认标志" prop="servedDefaultFlag">
        <el-select v-model="queryParams.servedDefaultFlag" placeholder="请选择默认标志" clearable>
          <el-option
            v-for="dict in dict.type.service_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="servedRemark">
        <el-input
          v-model="queryParams.servedRemark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['service:served:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['service:served:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['service:served:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['service:served:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="servedList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="被服务人主键ID" align="center" prop="servedId" />
      <el-table-column label="绑定用户的主键ID" align="center" prop="servedUserid" />
      <el-table-column label="姓名" align="center" prop="servedName" />
      <el-table-column label="身份证号" align="center" prop="servedIdnumber" />
      <el-table-column label="手机号码" align="center" prop="servedTelephonenumber" />
      <el-table-column label="地址" align="center" prop="servedAddress" />
      <el-table-column label="就诊卡号" align="center" prop="servedVisitid" />
      <el-table-column label="用户性别" align="center" prop="servedSex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.servedSex"/>
        </template>
      </el-table-column>
      <el-table-column label="与被服务人关系" align="center" prop="servedConnect" />
      <el-table-column label="要求服务日期" align="center" prop="servedDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.servedDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="要求服务时间" align="center" prop="servedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.servedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="servedCreateBy" />
      <el-table-column label="创建时间" align="center" prop="servedCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.servedCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="servedUpdateBy" />
      <el-table-column label="更新时间" align="center" prop="servedUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.servedUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="启用标志" align="center" prop="servedUseFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_yes_no" :value="scope.row.servedUseFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="删除标志" align="center" prop="servedDelFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_yes_no" :value="scope.row.servedDelFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="默认标志" align="center" prop="servedDefaultFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_yes_no" :value="scope.row.servedDefaultFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="servedRemark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['service:served:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['service:served:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改被服务人信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="绑定用户的主键ID" prop="servedUserid">
          <el-input v-model="form.servedUserid" placeholder="请输入绑定用户的主键ID" />
        </el-form-item>
        <el-form-item label="姓名" prop="servedName">
          <el-input v-model="form.servedName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="servedIdnumber">
          <el-input v-model="form.servedIdnumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="手机号码" prop="servedTelephonenumber">
          <el-input v-model="form.servedTelephonenumber" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="地址" prop="servedAddress">
          <el-input v-model="form.servedAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="就诊卡号" prop="servedVisitid">
          <el-input v-model="form.servedVisitid" placeholder="请输入就诊卡号" />
        </el-form-item>
        <el-form-item label="用户性别" prop="servedSex">
          <el-select v-model="form.servedSex" placeholder="请选择用户性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="与被服务人关系" prop="servedConnect">
          <el-input v-model="form.servedConnect" placeholder="请输入与被服务人关系" />
        </el-form-item>
        <el-form-item label="要求服务日期" prop="servedDate">
          <el-date-picker clearable
            v-model="form.servedDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择要求服务日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="要求服务时间" prop="servedTime">
          <el-date-picker clearable
            v-model="form.servedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择要求服务时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建者" prop="servedCreateBy">
          <el-input v-model="form.servedCreateBy" placeholder="请输入创建者" />
        </el-form-item>
        <el-form-item label="创建时间" prop="servedCreateTime">
          <el-date-picker clearable
            v-model="form.servedCreateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新者" prop="servedUpdateBy">
          <el-input v-model="form.servedUpdateBy" placeholder="请输入更新者" />
        </el-form-item>
        <el-form-item label="更新时间" prop="servedUpdateTime">
          <el-date-picker clearable
            v-model="form.servedUpdateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="启用标志" prop="servedUseFlag">
          <el-select v-model="form.servedUseFlag" placeholder="请选择启用标志">
            <el-option
              v-for="dict in dict.type.service_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="删除标志" prop="servedDelFlag">
          <el-select v-model="form.servedDelFlag" placeholder="请选择删除标志">
            <el-option
              v-for="dict in dict.type.service_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="默认标志" prop="servedDefaultFlag">
          <el-select v-model="form.servedDefaultFlag" placeholder="请选择默认标志">
            <el-option
              v-for="dict in dict.type.service_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="servedRemark">
          <el-input v-model="form.servedRemark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listServed, getServed, delServed, addServed, updateServed } from "@/api/service/served";

export default {
  name: "Served",
  dicts: ['service_yes_no', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 被服务人信息表格数据
      servedList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeServedDate: [],
      // 备注时间范围
      daterangeServedTime: [],
      // 备注时间范围
      daterangeServedCreateTime: [],
      // 备注时间范围
      daterangeServedUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        servedUserid: null,
        servedName: null,
        servedIdnumber: null,
        servedTelephonenumber: null,
        servedAddress: null,
        servedVisitid: null,
        servedSex: null,
        servedConnect: null,
        servedDate: null,
        servedTime: null,
        servedCreateBy: null,
        servedCreateTime: null,
        servedUpdateBy: null,
        servedUpdateTime: null,
        servedUseFlag: null,
        servedDelFlag: null,
        servedDefaultFlag: null,
        servedRemark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        servedName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        servedIdnumber: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        servedAddress: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询被服务人信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeServedDate && '' != this.daterangeServedDate) {
        this.queryParams.params["beginServedDate"] = this.daterangeServedDate[0];
        this.queryParams.params["endServedDate"] = this.daterangeServedDate[1];
      }
      if (null != this.daterangeServedTime && '' != this.daterangeServedTime) {
        this.queryParams.params["beginServedTime"] = this.daterangeServedTime[0];
        this.queryParams.params["endServedTime"] = this.daterangeServedTime[1];
      }
      if (null != this.daterangeServedCreateTime && '' != this.daterangeServedCreateTime) {
        this.queryParams.params["beginServedCreateTime"] = this.daterangeServedCreateTime[0];
        this.queryParams.params["endServedCreateTime"] = this.daterangeServedCreateTime[1];
      }
      if (null != this.daterangeServedUpdateTime && '' != this.daterangeServedUpdateTime) {
        this.queryParams.params["beginServedUpdateTime"] = this.daterangeServedUpdateTime[0];
        this.queryParams.params["endServedUpdateTime"] = this.daterangeServedUpdateTime[1];
      }
      listServed(this.queryParams).then(response => {
        this.servedList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        servedId: null,
        servedUserid: null,
        servedName: null,
        servedIdnumber: null,
        servedTelephonenumber: null,
        servedAddress: null,
        servedVisitid: null,
        servedSex: null,
        servedConnect: null,
        servedDate: null,
        servedTime: null,
        servedCreateBy: null,
        servedCreateTime: null,
        servedUpdateBy: null,
        servedUpdateTime: null,
        servedUseFlag: null,
        servedDelFlag: null,
        servedDefaultFlag: null,
        servedRemark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeServedDate = [];
      this.daterangeServedTime = [];
      this.daterangeServedCreateTime = [];
      this.daterangeServedUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.servedId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加被服务人信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const servedId = row.servedId || this.ids
      getServed(servedId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改被服务人信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.servedId != null) {
            updateServed(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addServed(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const servedIds = row.servedId || this.ids;
      this.$modal.confirm('是否确认删除被服务人信息编号为"' + servedIds + '"的数据项？').then(function() {
        return delServed(servedIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('service/served/export', {
        ...this.queryParams
      }, `served_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
